<?php
   /******************************************  
    * 获取商品分类
    * 只存入了淘宝的1级分类和2级分类
    *****************************************/

	require_once ( 'config.inc.php' );
	
	/**
	 * 商品一级分类，1-68(60个父分类)
	 */
	$DBInstance = new DB();	
	$Taoapi = new Taoapi;
	$Taoapi->method = 'taobao.itemcats.get';
	$Taoapi->parent_cid = '0';
	$Taoapi->fields = 'cid,parent_cid,name,is_parent,status,sort_order';	
	$TaobaoData = $Taoapi->Send('get','xml')->getArrayData();
    
	$m = '0';
	
    foreach ($TaobaoData['item_cats']['item_cat'] as $key => $item){
        $sql = "select * from product_category  where taobao_cid=".$item['cid'];
        $res = $DBInstance->GetRow($sql);
        if( $res ===  false ){
            if($item['is_parent']=='true')
            {
                    $m = '1';
            }else{
                    $m = '0';
            }

            $sql = "insert into product_category (cate_name,is_parent,parent_id,taobao_cid) values('".$item['name']."'".",".$m.",".$item['parent_cid'].",".$item['cid'].")";
            $DBInstance->Insert($sql);
        }
	}			
	$DBInstance->Disconnect();	
	
	echo 'finished';
	
	/**
	 * 商品二级分类，69-1138(404个父分类)
	 */		
	$DBInstance = new DB();
    $todoArray = $DBInstance->GetPlan("select * from product_category where is_parent=1");
	$Taoapi = new Taoapi;
	foreach($todoArray as $k => $v){//循环有二级分类的一级分类
		
    		
		$Taoapi->method = 'taobao.itemcats.get';
		$Taoapi->fields = 'cid,parent_cid,name,is_parent,status,sort_order';	
    	$Taoapi->parent_cid = $v['taobao_cid'];
    	$TaobaoData = $Taoapi->Send('get','xml')->getArrayData();
    			
		if($Taoapi->getErrorInfo())						 
		{					
			echo "API获取数据遇到错误,错误提示是:";
			print_r($Taoapi->getErrorInfo());
			die();
		}else{
					
			if(isset($TaobaoData['item_cats'])&&isset($TaobaoData['item_cats']['item_cat'])){
	    		foreach ($TaobaoData['item_cats']['item_cat'] as $key => $item)
	    		{ 		
	
                    $sql = "select * from product_category  where taobao_cid=".$item['cid'];
                    $res = $DBInstance->GetRow($sql);
                    if( $res ===  false ){
                        if($item['is_parent']=='true')
                        {
                            $m = '1';
                        }else{
                            $m = '0';
                        }
                        $sql = "insert into product_category (cate_name,is_parent,parent_id,taobao_cid) values('".$item['name']."'".",".$m.",".$v['id'].",".$item['cid'].")";					
                        $DBInstance->Insert($sql);
                    }
				}
			}
		}		
	}	

	/**
	 * 商品三级分类 - 数据库里没有用到
	 */		
	$DBInstance = new DB();
	$todoArray = $DBInstance->GetPlan("select * from product_category where is_parent=1 and parent_id<>0");
	$Taoapi = new Taoapi;
	foreach($todoArray as $k => $v){//循环有二级分类的一级分类
		
    		
		$Taoapi->method = 'taobao.itemcats.get';
		$Taoapi->fields = 'cid,parent_cid,name,is_parent,status,sort_order';	
    	$Taoapi->parent_cid = $v['taobao_cid'];
    	$TaobaoData = $Taoapi->Send('get','xml')->getArrayData();
    			
    	print_r($TaobaoData);
    	die();
		if($Taoapi->getErrorInfo())						 
		{					
			echo "API获取数据遇到错误,错误提示是:";
			print_r($Taoapi->getErrorInfo());
			die();
		}else{
					
			if(isset($TaobaoData['item_cats'])&&isset($TaobaoData['item_cats']['item_cat'])){
						
	    		foreach ($TaobaoData['item_cats']['item_cat'] as $key => $item)
	    		{ 		
                    $sql = "select * from product_category  where taobao_cid=".$item['cid'];
                    $res = $DBInstance->GetRow($sql);
                    if( $res ===  false ){
                        if($item['is_parent']=='true')
                        {
                            $m = '1';
                        }else{
                            $m = '0';
                        }
                        $sql = "insert into product_category (cate_name,is_parent,parent_id,taobao_cid) values('".$item['name']."'".",".$m.",".$v['id'].",".$item['cid'].")";					
                        $DBInstance->Insert($sql);
                    }	
				}
			}
		}		
	}
?>